<?php

use Core\AdminController;
use Models\Order;
use Models\Order_item;
use Models\Money_log;
use Models\Item;
use Models\Product;
use Models\Refund;

class UstatisticsController extends AdminController
{

	public function index(){
		$start_date = $this->input->post('start_date');
		$end_date = $this->input->post('end_date');
		$this->load->view('admin/ustatistics/index');

	}

	public function today(){
		$today = date('Y-m-d',time());
		if($this->input->get('day')) $today = $this->input->get('day');
		$order = new Order;
		$money_log = new Money_log;
		$orders = $order->select('sum(total) as total,sum(refund_fee) as refund_fee,type')->where('create_time>=',$today)->where_in('status',[1,2,3])->group_by('type')->get();
		$data = [
			'total'=>0,
			'online'=>0,
			'offline'=>0,
			'refund_fee'=>0,
		];
		if($orders){
			foreach ($orders as $t) {
				$data['total'] += $t['total'];
				$data['refund_fee'] += $t['refund_fee'];
				if($t['type'] == 0) $data['online'] += $t['total'];
				else $data['offline'] += $t['total'];
			}
		}
		// 总额
		$data['total_detail'] = $order->select('order_id,total,create_time')->order_by('id desc')->limit(100)->get();
		// 线上
		$data['online_detail'] = $order->select('order_id,total,create_time')->where('type!=0')->order_by('id desc')->limit(100)->get();
		// 线下
		$data['offline_detail'] = $order->select('order_id,total,create_time')->where('type=0')->order_by('id desc')->limit(100)->get();
		// 退款
		$data['refund_detail'] = $order->select('order_id,refund_fee as total,create_time')->where('refund_fee > 0')->order_by('id desc')->limit(100)->get();
		//充值
		$charge = $money_log->select('sum(num) as total')->where('create_time>=',$today)->where_in('type',[2,3])->get();
		$data['charge'] = $charge?$charge[0]['total']:0;
		$data['charge_detail'] = $money_log->select('num,create_time,desc')->where('create_time>=',$today)->where_in('type',[2,3])->get();
		//vat税
		$vat = (new Order_item)->select("sum((order_items.qty-order_items.refund_qty)*items.tax) as fee")
							   ->join('orders','orders.id = order_items.order_id')
							   ->join('items','items.id = order_items.item_id')
							   ->where('items.tax>',0)
							   ->where('orders.create_time>=',$today)
							   ->where_in('orders.status',[1,2,3])
							   ->get();
		$data['vat'] = $vat?$vat['0']['fee']:0;
		$data['vat_detail'] = (new Order_item)->select('items.title,(order_items.qty-order_items.refund_qty) as qty,items.tax')
			->join('orders','orders.id = order_items.order_id')
			->join('items','items.id = order_items.item_id')
			->where('items.tax>',0)
			->where('orders.create_time>=',$today)
			->where_in('orders.status',[1,2,3])
			->get();

		$order_modes = $order->select('sum(pay_fee) as pay_fee,sum(money) as money,,pay_mode')->where('create_time>=',$today)->where_in('status',[1,2,3])->group_by('pay_mode')->get();
		$money_modes = $money_log->select('sum(num) as total,pay_type as pay_mode')->where('create_time>=',$today)->where_in('type',[2,3])->group_by('pay_type')->get();
		$refunds = (new Refund)->select('sum(money) as money,pay_mode')->where('create_time>=',$today)->where('status',3)->group_by('pay_mode')->get();

		$sr = CONFIG('basic.pay_types');
		foreach ($order_modes as $om) {
			if($om['pay_mode'] > 0){
				$sr[$om['pay_mode']]['money'] = $om['pay_fee'];
			}else{
				$sr[$om['pay_mode']]['money'] = $om['money'];
			}

		}
		foreach ($money_modes as $om) {
			if(isset($sr[$om['pay_mode']]['money'])) $sr[$om['pay_mode']]['money'] += $om['total'];
			else $sr[$om['pay_mode']]['money'] = $om['total'];
		}

		$data['refund_balance'] = 0;
		$data['refund_cash'] = 0;

		foreach ($refunds as $r) {
			if($r['pay_mode'] == 0){
				$data['refund_balance'] = $r['money'];
			}else{
				$data['refund_cash'] = $r['money'];
			}
		}
		$data['sr'] = $sr;
		$data['today'] = $today;

		//$this->output->enable_profiler(TRUE);
		$this->load->view('admin/ustatistics/today', $data, FALSE);
	}

	public function getLineData(){
		$start_date = date('Y-m-d',strtotime("-7 days"));
		$end_date = date('Y-m-d H:i:s');
		$order = new Order;
		//select sum(total-refund_fee),type,DATE_FORMAT(create_time, '%Y-%m-%d') FROM orders group by DATE_FORMAT(create_time, '%Y-%m-%d'),type
		$orders = $order->select("sum(total-refund_fee) as fee,type,DATE_FORMAT(create_time, '%Y-%m-%d') as date")
			  ->where_in('status',[1,2,3])
			  ->where('create_time >=',$start_date)
			  ->group_by("DATE_FORMAT(create_time, '%Y-%m-%d'),type")
			  ->get();

		$datetime = strtotime($start_date);
		$online_data = [];
		$offline_data = [];
		$dates = [];
		while ($datetime <= time()) {
			$online_data[date('Y-m-d',$datetime)] = 0;
			$offline_data[date('Y-m-d',$datetime)] = 0;
			$dates[] = date('m-d',$datetime);
			$datetime += 3600*24;

		}

		foreach ($orders as $o) {
			if($o['type'] == 0){
				$online_data[$o['date']] = $o['fee'];
			}else{
				$offline_data[$o['date']] = $o['fee'];
			}
		}

		$this->response(['dates'=>$dates,'online'=>array_values($online_data),'offline'=>array_values($offline_data)]);

	}

}
